import { getRequest } from "@/api/request";
import { AxiosResponse } from "axios";
const REQUEST_URL = {
  topSong: "/playlist/detail?id=2219770152",
  songUrl: "/song/url",
  loginState: "/login/status",
};

const filter = (res: AxiosResponse) => {
  const it = res.data.playlist.tracks[Math.floor(Math.random() * (10 - 0)) + 0];
  const musicInfo = {
    musicId: it.id,
    picUrl: it.al.picUrl,
    musicName: it.name,
    song: it.ar[0].name,
  };
  return musicInfo;
};

const handleLoginState = (res: AxiosResponse) => {
  const userInfo = {
    nickname: res.data.data.profile.nickname,
    userId: res.data.data.profile.userId,
    avatarUrl: res.data.data.profile.avatarUrl,
  };
  return userInfo;
};

const handleSongUrl = (res: AxiosResponse) => {
  return res.data.data[0].url;
};

export const getTopSong = () => {
  return getRequest(REQUEST_URL.topSong).then(filter);
};

export const getSongUrl = (id: number | string | null) => {
  return getRequest(REQUEST_URL.songUrl + `?id=${id}`).then(handleSongUrl);
};

export const getLoginState = () => {
  return getRequest(REQUEST_URL.loginState).then(handleLoginState);
};
